package com.engine.jz.thread;

import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.fastjson.JSONObject;
import com.engine.jz.util.CommonUtil;
import com.engine.jz.util.TelpoUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2024/3/12
 * @Description 处理餐补数据
 * @Author AdminZm
 */
public class TBDataThread extends BaseBean implements Runnable{

    BaseBean baseBean = new BaseBean();

    private String yg;

    private String ksrq;

    private String jsrq;

    public TBDataThread(String yg, String ksrq, String jsrq) {
        this.yg = yg;
        this.ksrq = ksrq;
        this.jsrq = jsrq;
    }

    @Override
    public void run() {
        baseBean.writeLog("TBDataThread:" + yg + "、" + ksrq);
        try {
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            // 获取工号与id对应
            Map<String, String> ghAndIdMap = new HashMap<>();
            Map<String, String> idAndGhMap = new HashMap<>();
            rs.execute("SELECT id, workcode FROM hrmresource WHERE workcode is not null");
            while (rs.next()) {
                String id = rs.getString("id");
                String workcode = rs.getString("workcode");
                if (StringUtils.isEmpty(id) || StringUtils.isEmpty(workcode)) {
                    continue;
                }
                workcode = CommonUtil.removeLeadingZeros(workcode);
                if (StringUtils.isEmpty(workcode)) {
                    continue;
                }
                ghAndIdMap.put(workcode, id);
                idAndGhMap.put(id, workcode);
            }
            if (CollectionUtils.isEmpty(idAndGhMap)) {
                baseBean.writeLog("getTbDoorData idAndGhMap is null.");
                return;
            }

            String modeId = rs.getPropValue("jzjg_zm_prop", "kqsjtbjl_mode_id");
            String cusId = rs.getPropValue("jzjg_zm_prop", "kqsjtbjl_cus_id");
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");

            TelpoUtils telpoUtils = new TelpoUtils();

            String startTime = ksrq + " 00:00:01";
            String endTime = jsrq + " 23:59:59";

            Map<String, Object> mapTemp = new HashMap<>();
            if (StringUtils.isNotEmpty(yg)) {
                String[] ygArr = yg.split(",");
                List<String> ygList = new ArrayList<>();
                for (String ygT : ygArr) {
                    String ghTemp = idAndGhMap.get(ygT);
                    if (StringUtils.isNotEmpty(ghTemp)) {
                        ygList.add(idAndGhMap.get(ygT));
                    }
                }
                mapTemp.put("pin", ygList);
            }
            mapTemp.put("photo", 0);
            mapTemp.put("start", startTime);
            mapTemp.put("end", endTime);

            int corePoolSize = 5;
            int maxPoolSize = 10;
            ExecutorService executorService = ThreadUtil.newExecutor(corePoolSize, maxPoolSize);

            // 获取天波考勤设备信息
            rs.execute("SELECT * FROM uf_kqsbdzgxgljmb WHERE sblx = 0 AND sbip is not null AND sftskqsj = 0");
            while (rs.next()) {
                String id = rs.getString("id");
                String sbip = "192.168.1.43";
                String sbxlh = rs.getString("sbxlh");
                if (StringUtils.isEmpty(id) || StringUtils.isEmpty(sbip) || StringUtils.isEmpty(sbxlh)) {
                    continue;
                }
                Map<String, Object> paramMap = new HashMap<>();
                List<String> snList = new ArrayList<>();
                snList.add(sbxlh);
                paramMap.put("SN", snList);
                long taskId = System.currentTimeMillis();
                paramMap.put("taskId", taskId);
                paramMap.put("type", "queryRecords");
                paramMap.put("Data", mapTemp);
                rs1.executeUpdate("INSERT INTO uf_kqsjtbjlb (sb, rwid, qqcs, formmodeid,MODEUUID,modedatacreater," +
                                "modedatacreatertype, modedatacreatedate, modedatacreatetime) VALUES (?,?,?,?,?,?,?,?,?)", id, taskId,
                        JSONObject.toJSONString(paramMap), modeId, UUID.randomUUID(), "1", "0", sdfDate.format(new Date()), sdfTime.format(new Date()));
                telpoUtils.queryTelpoMj(sbip, paramMap, String.valueOf(taskId));

                Thread.sleep(2000);
            }
        } catch (Exception e) {
            baseBean.writeLog("TBDataThread error:" + e.getMessage());
        }
    }
}
